package com.nickstumpos.android.GmailWidgetPlus.widget.presentation.managers;

import android.app.PendingIntent;
import android.appwidget.AppWidgetManager;
import android.content.Context;
import android.view.View;
import android.widget.RemoteViews;

import com.nickstumpos.android.GmailWidgetPlus.service.IntentService.GMWPIntentService;
import com.nickstumpos.android.GmailWidgetPlus.widget.sizes.WidgetSize;
import com.nickstumpos.android.GmailWidgetPlus.widget.skins.WidgetSkins;

public abstract class AWidgetUIManager implements WidgetUIManager {
	private RemoteViews				widgetLayout	= null;
	private final Context			context;
	private final int				widgetId;

	private boolean					haveAccount;
	private final String			account;
	private final GMWPIntentService	gmwpIntentService;

	protected AWidgetUIManager(final Context context,
			final String account,
			final int widgetid) {
		this.context = context;
		this.widgetId = widgetid;
		this.haveAccount = (account != null) ? true : false;
		this.account = account;
		this.gmwpIntentService = new GMWPIntentService(context,
				account,
				widgetid,
				this.getSize());
	}

	protected void commitUpdate() {

		final AppWidgetManager manager = AppWidgetManager.getInstance(this.getContext());
		manager.updateAppWidget(this.widgetId, this.getRemoteViews());
		this.widgetLayout = null;
	}

	public String getAccount() {
		return this.account;
	}

	public Context getContext() {

		return this.context;
	}

	public GMWPIntentService getGMWPIntents() {
		return this.gmwpIntentService;
	}

	private RemoteViews getRemoteViews() {

		if (this.widgetLayout == null) {
			this.widgetLayout = new RemoteViews(this.getContext()
					.getPackageName(), this.getWidgetSkin().getLayout());
		}
		return this.widgetLayout;
	}

	protected abstract WidgetSize getSize();

	protected int getWidgetId() {

		return this.widgetId;
	}

	protected abstract WidgetSkins getWidgetSkin();

	protected void hideView(final int viewID) {
		this.getRemoteViews().setViewVisibility(viewID, View.GONE);
	}

	public boolean isHaveAccount() {
		return this.haveAccount;
	}

	protected abstract void loadChange();

	protected void setClickAction(final int viewID, final PendingIntent action) {
		this.getRemoteViews().setOnClickPendingIntent(viewID, action);
	}

	public void setHaveAccount(final boolean haveAccount) {
		this.haveAccount = haveAccount;
	}

	protected void setViewText(final int viewID, final String text) {
		this.getRemoteViews().setTextViewText(viewID, text);
	}

	protected void showView(final int viewID) {
		this.getRemoteViews().setViewVisibility(viewID, View.VISIBLE);
	}
}
